//
//  HNSH_HomeFunctionCell.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/23.
//

#import "HNSH_HomeFunctionCell.h"
#import "HNSH_HomeFunctionItem.h"

@interface HNSH_HomeFunctionCell ()

/* bgView */
@property (nonatomic, strong) UIView *bgView;

@end

@implementation HNSH_HomeFunctionCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        [self configUI];
    }
    return self;
}

- (void)setModel:(HNSH_HomeViewModel *)model {
    _model = model;
    
    [self.bgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    CGFloat width = 70;
    CGFloat height = 67;
    CGFloat leftSpece = 20;
    CGFloat spece = (kScreenWidth-width*5-leftSpece*2)/5;
    for (int i = 0; i < model.shortcuts.count; i ++) {
        HNSH_HomeViewBannerItem *item = model.shortcuts[i];
        HNSH_HomeFunctionItem *itemView = [[HNSH_HomeFunctionItem alloc]initWithFrame:CGRectMake(leftSpece + (width + spece) * (i % 5), 12 + (height + 16) * (i / 5), width, height)];
        itemView.titleLabel.text = item.name;
        [itemView.imgV sd_setImageWithURL:[NSURL URLWithString:item.image_url]];
        [self.bgView addSubview:itemView];
        itemView.tag = i;
        itemView.clickBlock = ^{
            [NSString jumpToolsWithJumpType:item.jump_type jump_name:item.name jump_content:item.jump_content jump_params:item.jump_params];
        };
    }
}

- (void)configUI {
    [self.contentView addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
        make.top.bottom.mas_equalTo(0);
    }];
}

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = [UIColor appWhiteLabelColor];
        _bgView.layer.cornerRadius = 8.0f;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}

@end
